package com.niiwoo.civet.account.service.local.openAccount2.lanmao;

import com.niiwoo.civet.account.dao.entity.AccountBase;
import com.niiwoo.civet.account.dao.entity.OpenAccountOrder;
import com.niiwoo.civet.account.service.local.AccountService;
import com.niiwoo.civet.account.service.local.openAccount2.dto.CreateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.dto.RegisterCreateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.type.RegisterTypeable;
import com.niiwoo.civet.base.constant.OrgConstant;
import com.niiwoo.tripod.lanmao.component.LanMaoGatewayService;
import com.niiwoo.tripod.lanmao.enums.AuthenticateValidTypeEnum;
import com.niiwoo.tripod.lanmao.enums.CredentialsTypeEnum;
import com.niiwoo.tripod.lanmao.enums.UserLimitTypeEnum;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import com.niiwoo.tripod.lanmao.request.PersonalBindBankCardRegisterRequest;
import com.niiwoo.tripod.lanmao.response.LanMaoGatewayDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class RegisterLanMaoHandler implements OpenAccountLanMaoOperation<OpenAccountOrder>, RegisterTypeable {

    @Autowired
    LanMaoGatewayService lanMaoGatewayService;

    @Autowired
    AccountService accountService;

    @Override
    public LanMaoGatewayDTO generateLanMaoGatewayDTO(CreateRequest request, OpenAccountOrder order) {
        // 是否存在待激活账户
        /*Optional<AccountBase> unactivatedOpt = Optional.ofNullable(accountService.listAccountByUserId(order.getUserId()))
                .filter(list -> !list.isEmpty())
                .flatMap(list -> list.stream().filter(a -> a.getImportUserActiva().equals(AccountActiveEnum.UNACTIVATED.getValue())).findFirst());*/

        RegisterCreateRequest registerCreateRequest = (RegisterCreateRequest) request;
        // 请求
        PersonalBindBankCardRegisterRequest personalBindBankCardRegisterRequest = new PersonalBindBankCardRegisterRequest();
        personalBindBankCardRegisterRequest.setPlatformUserNo(order.getAccountNo());
        personalBindBankCardRegisterRequest.setRequestNo(order.getRequestNo());
        personalBindBankCardRegisterRequest.setRealName(order.getRealName());
        personalBindBankCardRegisterRequest.setIdCardNo(registerCreateRequest.getIdCardNo());
        personalBindBankCardRegisterRequest.setBankcardNo(registerCreateRequest.getBankCardNo());
        personalBindBankCardRegisterRequest.setMobile(registerCreateRequest.getBankCardMobileNo());
        personalBindBankCardRegisterRequest.setIdCardType(CredentialsTypeEnum.PRC_ID);
        personalBindBankCardRegisterRequest.setUserRole(UserRoleEnum.enumOf(order.getUserRole()));
        personalBindBankCardRegisterRequest.setCheckType(AuthenticateValidTypeEnum.LIMIT);
        // 没有待激活的账户则校验ID唯一性
        //if (!unactivatedOpt.isPresent()) {

        // 用户所有账户
        List<AccountBase> userAccountList = accountService.listAccountByUserId(order.getUserId());
//        if(userAccountList.size() == 0){
//            personalBindBankCardRegisterRequest.setUserLimitType(UserLimitTypeEnum.ID_CARD_NO_UNIQUE);
//        }
        personalBindBankCardRegisterRequest.setAuthList(order.getAuthList());

        // 你我金融自有平台，默认开通集团账户
        if(OrgConstant.NIIWOO_ORG_ID.equals(request.getOrgId())){
            personalBindBankCardRegisterRequest.setGroupAccount("TRUE"); // 默认开通集团账户
        }
        personalBindBankCardRegisterRequest.setTimestamp(new Date());
        personalBindBankCardRegisterRequest.setNiiwooRedirectUrl(request.getRedirectUrl());
        personalBindBankCardRegisterRequest.setFailTime(LanMaoHelper.getFixFailTime());
        personalBindBankCardRegisterRequest.setAmount(LanMaoHelper.getFixLimitAmount());
        return lanMaoGatewayService.personalBindBankCardRegister(personalBindBankCardRegisterRequest);
    }
}
